<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Column extends Model
{
    //软删除
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    protected $autoWriteTimestamp = TRUE;
    protected $updateTime = FALSE;
    protected $insert = ['status' => 1];

    //关联文章表
    public function article()
    {
        return $this->hasMany('Article', 'column_id', 'id');
    }

    //事件
    protected static function init()
    {
        self::beforeDelete(function ($thisObj) {
            //删除栏目时把对应的关联也删除

            $aid = [];
            foreach ($thisObj->article as $v){
                $aid[] = $v['id'];
            }

//            halt($aid);
            if (!empty($aid)){
                Article::destroy($aid);
            }
        });
    }

    /*
     * 数据列表
     * */
    public static function mAll($page = 1, $limit = 15, $data = [])
    {
        if (0 === count($data) && $data === []) {
            $rel = self::order('sort')->paginate($limit, FALSE, ['page' => $page])->toArray();
        } else {
            //时间条件判断
            if (isset($data['start'])) {
                $map['create_time'] = ['> time', $data['start']];
            } else if (isset($data['end'])) {
                $map['create_time'] = ['< time', $data['end']];
            } else if (isset($data['start']) && isset($data['end'])) {
                $map['create_time'] = ['between time', [$data['start'], $data['end']]];
            }
            //管理员判断
            if (isset($data['name'])) {
                $map['name'] = ['like', '%' . $data['name'] . '%'];
            }
            $rel = self::where($map)->order('sort')->paginate($limit, FALSE, ['page' => $page, 'query' => $map])->toArray();
        }

        if (FALSE != $rel) {

            if (0 != $rel['total']) {
                $data['code'] = 0;
                $data['msg'] = '数据请求成功！';
                $data['count'] = $rel['total'];
                $data['data'] = infinite_classify_1w($rel['data']);
            } else {
                $data['code'] = 0;
                $data['msg'] = '数据请求成功！';
            }
            return $data;
        }
        return return_json(0, '数据请求失败');
    }

    /*数据增加与修改
     * */
    public static function mSave($data = [], $method = ['create', '增加'])
    {
        if ('update' == $method[0]) {
            $rel = self::update($data, ['id' => $data['id']], TRUE);
        } else {
            $rel = self::create($data, TRUE);
        }
        if (FALSE !== $rel) {
            return return_json(0, $method[1] . '成功！');
        }
        return return_json(1, $method[1] . '失败！');
    }

    /*删除
     * */
    public static function mDel($data = [])
    {
        if (1 !== count($data) && $data) {
            $data[0] = implode(',', $data);
        }
        $rel = self::destroy($data[0]);

        if (FALSE != $rel) {
            return return_json(0, '删除成功！', 0);
        } else {
            return return_json(1, '删除失败！');
        }
    }

    /*
     * 单元格修改
     * */
    public static function mCellEdit($data = [])
    {
        $rel = self::update($data);
        if (FALSE != $rel) {
            return return_json(0, '修改成功！');
        } else {
            return return_json(1, '修改失败！');
        }
    }
}